package com.cnswhy.cloud.server.core.channel.verify.service.impl;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.server.core.channel.verify.model.VerifyRequest;
import com.cnswhy.cloud.server.core.channel.verify.model.VerifyResponse;
import com.cnswhy.cloud.server.core.channel.verify.service.AliyunVerifyService;

import com.cnswhy.cloud.server.core.channel.verify.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class AliyunVerifyServiceImpl implements AliyunVerifyService {


    public VerifyResponse verify4(VerifyRequest verifyRequest) {
        log.info("enter info verify4:{}", JSON.toJSONString(verifyRequest));
        String host = "https://aliyun-bankcard4-verify.apistore.cn";
        String path = "/bank4";
        String method = "GET";
        String appcode = "4cdedb8fa200492a9eef946ff64fc8e7";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("Mobile", verifyRequest.getMobile());
        querys.put("bankcard", verifyRequest.getBankcard());
        querys.put("cardNo", verifyRequest.getCardNo());
        querys.put("realName", verifyRequest.getRealName());

        VerifyResponse verifyResponse = null;
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body

            String responseMsg = new String(EntityUtils.toString(response.getEntity()));
            log.info(responseMsg);
            verifyResponse = JSON.parseObject(responseMsg, VerifyResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (verifyResponse == null) {
            verifyResponse = new VerifyResponse();
            verifyResponse.setError_code(-1);
            verifyResponse.setReason("网络异常");
        }
        if (verifyResponse.getError_code() != 0) {
            if (StringUtils.isEmpty(verifyResponse.getReason())) {
                verifyResponse.setReason("网络异常");
            }
        }
        log.info("exit verify4:{}", JSON.toJSONString(verifyResponse));
        return verifyResponse;
    }
}
